<script>
	var citytableoption={
			toolbar: '#citytoolbar',//工具按钮用哪个容器
			clickToSelect: true,// 单击行即可以选中
			singleSelect: true, //设置true禁止多选
			sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            showPaginationSwitch: true,
			showExport: true,
			exportDataType: 'all',
			exportTypes: ['doc', 'excel'], //导出文件类型
			showToggle: true,  //是否显示视图切换按钮
			showRefresh: true,// 是否显示刷新按钮
			showColumns: true,//是否显示所有的列
			search: true, // 开启搜索功能
			pagination: true,// 开启分页功能
			sortName: "cid", // 设置默认排序为 name
			columns: [{				field: "checked",				checkbox: true				}, {				field: "isused",				title: "是否使用",				align: "center",				formatter : function (value, row, index) { switch(value) {case 1 :return "是"; default : return "否"}},				valign: "middle"				}, {				field: "city",				title: "市名",				align: "center",				valign: "middle"				}, {				field: "cid",				title: "市id",				visible: false,				align: "center",				valign: "middle"				}, {				field: "pid",				title: "省id",				visible: false,				align: "center",				valign: "middle"				}, {				field: "province",				title: "省名",				align: "center",				valign: "middle"			}],
			onlClickCell: function (field,value,row) {
					row.checked=true;
			},
			onCheck: function (row) {
				if (actions.includes("189")) {
					console.log(row)
					switch(row.isused) {
						case 0:$("#191").text("显示");break;
						case 1:$("#191").text("隐藏");break;
						default:$("#191").text("显示");break;
					}
					cityEditTable(row);
				}
			}
		}

	$(function() {
		checkaction(new Array("188","189","191","192","193"));

		$("#cityQueryTable").bootstrapTable(citytableoption);
		$("#cityQueryTable").bootstrapTable('showLoading');
		ajaxarr([{				url:"sysinfo/province_query",				data:{token:token},				title:"获取省名信息失败！",				func: function(data) {					var pid="<option value=''>请选择省名</option>";					if (data.info.total>0) {						for (i=0,j=data.info.rows.length;i<j;i++) { pid+="<option value='"+ data.info.rows[i].pid + "'>"+data.info.rows[i].province+"</option>";}					}					$("#txt_pid").html(pid);				$(".selectpicker").selectpicker("refresh");				}		}]);
		cityQueryTable();
		$(".selectpicker").selectpicker("refresh");
		$("#citymyModal").on("shown.bs.modal",function(e){ if ($("#txt_cid").attr("disabled")) { $("#txt_txt_pid").focus() } else {$("#txt_cid").focus()}; });
	});
	
	function cityAddTable() {//新增
		$("#txt_city").val("");		$("#txt_cid").val("");		$("#txt_cid").attr("disabled",false);		$("#txt_pid").selectpicker("val","");		$("#txt_pid").val("");
        $("#citymyModalLabel").html("新增");
        
	};
	
	function cityEditTable(row) {
		if (row==null) {
			if ($("#cityQueryTable").bootstrapTable('getSelections').length == 0) {
				alert("请先选择要修改的数据!");
				return;
			};
			row=$("#cityQueryTable").bootstrapTable('getSelections')[0];
		}
		if (row.city!=null) { $("#txt_city").val(row.city) } else { $("#txt_city").val("")};		if (row.cid!=null) { $("#txt_cid").val(row.cid) } else { $("#txt_cid").val("")};		$("#txt_cid").attr("disabled",true);		if (row.pid!=null) { $("#txt_pid").selectpicker("val",row.pid) } else { $("#txt_pid").selectpicker("val","")};		if (row.pid!=null) { $("#txt_pid").val(row.pid) } else { $("#txt_pid").val("")};
        $("#citymyModalLabel").html("修改");
        
	};
	
	function cityCommitTable() {
		var citycommitdata={
			city:$("#txt_city").val(),			cid:$("#txt_cid").val(),			pid:$("#txt_pid").val(),			pid:$("#txt_pid").val(),			token:token,
		};
		if ($("#citymyModalLabel").html()=="新增") {
			ajaxarr([{
				url:"sysinfo/city_add",
				title:"新增数据失败！",
				data:citycommitdata,
				func:function(data) {
					alert("新增数据成功！id："+data.info.id);
					cityQueryTable(data.info.id);
					cityAddTable();
				}
			}]);
		} else {
			ajaxarr([{
				url:"sysinfo/city_edit",
				title:"修改数据失败！",
				data:citycommitdata,
				func:function(data) {
					alert("修改数据成功！");
					cityQueryTable(citycommitdata.cid);
					
				}
			}]);		
		};
	};
	
	function cityDelTable() {
        var row = $("#cityQueryTable").bootstrapTable('getSelections')[0];
        if(row == null) {
            alert("请先选择要删除的数据!");
            return;
        };       
		if(confirm( "您真的确定要删除吗？") == true) {
			ajaxarr([{
				url: "sysinfo/city_del" ,
				title: "删除数据失败！",
				data:{token:token,cid : row.cid },
				func: function(data) {
					alert("删除成功！");
					cityQueryTable();
				}
			}]);
		}
	};
	
	function cityUndelTable() {
        var row = $("#cityQueryTable").bootstrapTable('getSelections')[0];
        if(row == null) {
            alert("请先选择要恢复的数据!");
            return;
        };       
		if(confirm( "您真的确定要恢复吗？") == true) {
			ajaxarr([{
				url: "sysinfo/city_undel" ,
				title: "恢复数据失败！",
				data:{token:token,cid : row.cid },
				func: function(data) {
					alert("恢复成功！");
					cityQueryTable();
				}
			}]);
		}
	}


	function cityQueryTable(id) {
		$("#cityQueryTable").bootstrapTable('showLoading');
		ajaxarr([{
				url:"sysinfo/city_query",
				title:"获取市信息失败，请刷新！",
				data:{token:token},
				func: function (data) {
					if (data.info.total==0) { $("#cityQueryTable").bootstrapTable("destroy").bootstrapTable(citytableoption); } 
					else {	
						$("#cityQueryTable").bootstrapTable('load', data.info.rows); 
						if (id) {
							for (var i=data.info.total;i>0;i--) {
								if (data.info.rows[i-1].cid==id) {
									$("#cityQueryTable").bootstrapTable('selectPage', Math.ceil(i/$("#cityQueryTable").bootstrapTable('getOptions').pageSize));
									$("#cityQueryTable").bootstrapTable('checkBy',{field:'cid',values:[data.info.rows[i-1].cid]})
									break;
								};							
							}
						} else {
							$("#cityQueryTable").bootstrapTable('checkBy',{field:'cid',values:[data.info.rows[0].cid]})
							cityEditTable(data.info.rows[0]);
						}
					};
					$("#cityQueryTable").bootstrapTable('hideLoading');
				}
		}]);		
	};
</script>
<table id="cityQueryTable" class='table-striped'></table>
<div id="citytoolbar" class="btn-group">
	<button id="188" type="button" style="display:none;" class="btn btn-default" onclick="cityAddTable()"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增</button>
	<button id="189" type="button" style="display:none;" class="btn btn-default" onclick="cityEditTable()"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改</button>
	<button id="191" type="button" style="display:none;" class="btn btn-default" onclick="cityDelTable()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除</button>
	<button id="193" type="button" style="display:none;" class="btn btn-default" onclick="cityUndelTable()"><span class="glyphicon glyphico-ok" aria-hidden="true"></span>恢复</button>
	<button id="192" type="button" style="display:none;" class="btn btn-default" onclick="cityQueryTable()"><span class="glyphicon glyphico-query" aria-hidden="true"></span>查询</button>
</div>
<div class="row" id="citymyModal" tabindex="-1" >
	<div class="col-12" >
		<div class="modal-content" >
			<div class="modal-header">
				<h4 class="modal-title" id="citymyModalLabel">新增</h4>
				
			</div>
			<div class="modal-body form-inline">
				<label for="txt_city" class="col-lg-3 text-center">市名</label><input id="txt_city" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_cid.focus()}}" type="text" placeholder="市名"/>				<label for="txt_cid" class="col-lg-3 text-center">市id</label><input id="txt_cid" class="form-control col-lg-3" disabled="disabled" onkeypress="{ if (event.keyCode==13) {txt_pid.focus()}}" type="text" placeholder="市id"/>				<label for="txt_pid" class="col-lg-3 text-center">省名</label><select id="txt_pid" class="selectpicker form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {SaveModal.focus()}}" title="请选择省id"></select>			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal" onclick="cityAddTable()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>清空</button>
				<button type="button" class="btn btn-primary" id="citySaveModal" onclick="cityCommitTable()"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存</button>
			</div>
		</div>
	</div>
</div>
